package com.safe.ocs.services;
/**
 * 将短信请假的信息即Vocation表写入到KQ_Leave表中
 * 
 */
import java.util.Date;
import java.util.List;
import java.util.TimerTask;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.safe.ocs.beans.KQ_Leave;
import com.safe.ocs.utils.HibernateUtil;

public class LeaveMsg extends TimerTask {

	@Override
	public void run() {
		Session session = HibernateUtil.getSession();
		Transaction tr = session.beginTransaction();
		String findVocation = "select person_ID,vocatefrom,convert(varchar(10),vocatefrom,120) as fromOld,vocateto,update_Time,convert(varchar(10),update_Time,120) as updateOld from Vocation";
		
		try{
			List vocationList = session.createSQLQuery(findVocation).list();
			
				for(int i=0;i<vocationList.size();i++){
					Object vocationArray[] = (Object[])vocationList.get(i);
					int person_ID = (Integer)vocationArray[0];					
					Date vocatefrom = (Date)vocationArray[1];
					String leave_Date =(String)vocationArray[2];//请假起始日期
					Date vocateto = (Date)vocationArray[3];					
					Date update_Time = (Date)vocationArray[4];//更新日期的所有时间
					String updates = (String)vocationArray[5];//更新日期的天数
					System.out.println(leave_Date);				
					float time1 = vocatefrom.getTime();
					float time2 = vocateto.getTime();
					float hour = (time2-time1)/(60*60*1000);
					System.out.println(hour); //请假的小时数
					KQ_Leave leave = new KQ_Leave();
					//String findLeave = "from KQ_Leave a  where a.person_ID=? and a.convert(varchar(10),update_Date,120)=?";
					String findLeave = "select * from KQ_Leave where person_ID=? and convert(varchar(10),update_Date,120)=?";
					Query query = session.createSQLQuery(findLeave).addEntity(KQ_Leave.class);
					query.setLong(0, person_ID);
					query.setString(1, updates);					
					List leaveList = query.list();
					System.out.println(leaveList.size());
					if(leaveList.size()==0){
						leave.setKind_No("J104");
						leave.setLeave_Date(leave_Date);
						leave.setLeave_Hour(hour);
						leave.setPerson_ID(person_ID);
						leave.setUpdate_Date(update_Time);
						leave.setOutLeave(false);
						System.out.println(leave.getKind_No()+leave.getLeave_Date()+leave.getLeave_Hour());
						session.save(leave);
					}
					
				}
			
			tr.commit();
			session.close();
		}catch(Exception e){
			tr.rollback();
			e.printStackTrace();
		}
		

	}

}
